package cn.tedu.home1ess.admin.traffic.exception.handler;


import cn.tedu.common.exception.ServiceException;
import cn.tedu.common.response.JsonResult;
import cn.tedu.common.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    //Service处理请求异常
    @ExceptionHandler
    public JsonResult doHandleServiceException(ServiceException e){
        log.error("捕获业务异常ServiceException: " + e);
        return JsonResult.fail(StatusCode.BAD_REQUEST_CATEGORY_NAME_EXIST);
    }

    //Argument处理请求异常
    @ExceptionHandler
    public JsonResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("捕获MethodArgumentNotValidException",e);
        return JsonResult.fail(StatusCode.BAD_REQUEST);
    }

    //处理运行时异常
    @ExceptionHandler
    public JsonResult handleRuntimeException(RuntimeException e){
        log.error("捕获RuntimeException",e);
        return JsonResult.fail(StatusCode.OPERATION_FAILED);
    }

    //处理全部异常，兜底
    @ExceptionHandler
    public JsonResult handleException(Throwable e){
        log.error("系统出现异常，捕获Throwable",e);
        return JsonResult.fail(StatusCode.THROWABLE_ERROR);
    }
}
